<?php
class Comment_model extends CI_Model{
	function __construct(){
		parent::__construct();
	}

	public function action($pid){
		
		$data['pid'] = $pid;
		$data['uid'] = $this->auth->uid;
		$data['uname'] = $this->auth->uname;
		$data['content'] = $this->form_validation->set_value('content');
		$data['time'] = date('Y-m-d H:i:s');
		if($this->db->insert('comment',$data)){
			return array('status'=>true);
		}else{
			return array('status'=>false,'msg'=>'评论失败，可能网络出错！');
		}
	}

	public function update($id){
		$data['name'] = $this->form_validation->set_value('name');
		$data['brand'] = $this->form_validation->set_value('brand');
		$data['image'] = $this->form_validation->set_value('image');
		$data['short_name'] = $this->form_validation->set_value('short_name');
		$data['cat'] = $this->form_validation->set_value('cat');
		$data['brif'] = addslashes(htmlspecialchars($this->form_validation->set_value('brif')));
		$data['description'] = addslashes(htmlspecialchars($this->form_validation->set_value('description')));
		$data['check_safe'] = addslashes(htmlspecialchars($this->form_validation->set_value('check_safe')));
		$data['product'] = addslashes(htmlspecialchars($this->form_validation->set_value('product')));
		$production_image = $this->input->post('production_image');
		if($production_image){
			$data['production_image'] = json_encode($production_image);
		}
		$this->db->where('id',$id);
		if($this->db->update('production',$data)){
			return array('status'=>true);
		}else{
			return array('status'=>false,'msg'=>'更新失败，可能网络出错！');
		}
	}


	public function get_comment($pid,$start=0,$limit=null){
		
		$this->db->where('pid',$pid);
		$this->db->order_by('time','DESC');
		$this->db->limit($limit,$start);

		$query = $this->db->get('comment');
		if($query->num_rows){
			return $query->result();
		} else {return false;}
	}

	public function get_comment_num($pid){
		$this->db->where('pid',$pid);
		return $this->db->count_all_results('comment');
	}
}